<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * wizard class to book a training
 *
 * @author: febusch
 */

class booking extends TMT_Controller {
    
    function __construct() 
    {
        parent::__construct();
        
        $this->load->helper('url', 'input', 'form');
		$this->set_content_view('booking/index');
    }
    
    public function index()
    {
        redirect('/catalogue');
    }
    
    public function select_training()
    {
    	$course_id = $this->input->post('course_id');
		if(
			//no course_id set
			!$course_id || 
			//no course with this id
			!($this->view_data['course_details']['row'] = Model\Course::find($course_id))
			) 
        {
            redirect('/catalogue');
        }
		

		$this->headline = 'Schritt 1: Auswahl des Teammitglieds';
		
		
		$teamroles = $this->employee->teamrole();
		$active_teamrole = $teamroles[$this->active_team];
		$team = $active_teamrole->team();
		$teammembers = $team->employee();
			
			
		foreach($teammembers as $teammember)
		{
			$contactdata = $teammember->contactdata();
			$this->view_data['teammembers'][$teammember->id] = $contactdata->surname.' , '.$contactdata->forename;
		}
		
		//Buttons
		$this->view_data['next'] = 'booking/select_bedarf/';
		$this->view_data['btn_abort'] = array('title' => 'Abbrechen', 'target' => '/catalogue', 'options' => 'class="btn"');
		$this->view_data['btn_next'] = array('title' => 'Weiter', 'options' => 'class="btn btn-success"');
		
		//Switch to toggle between 
		$this->view_data['step'] = 1;
		
		$this->view_data['hidden']['course_id'] = $course_id;
	}
    
    public function select_bedarf()
    {
    	$teammember_id = $this->input->post('teammember_id');
		$course_id = $this->input->post('course_id');
		$stope_id = $this->input->post('stope_id');
		if(!$course_id)
			redirect('catalogue');
			
        $this->headline = 'Schritt 2: Massnahme neu erstellen oder ersetzen';
		
        if($this->_set_course_details($course_id))
        {
            $this->view_data['next'] = 'booking/confirm/'.$course_id;
            
            $this->view_data['btn_abort'] = array('title' => 'Abbrechen', 'target' => '/catalogue', 'options' => 'class="btn"');
            $this->view_data['btn_next'] = array('title' => 'Weiter', 'options' => 'class="btn btn-success"');
           
            $employee = Model\Employee::find($teammember_id,FALSE);
            
            if($stope_id !== FALSE)
            {
            	$demand[] = Model\Stopedemand::find($stope_id);
            }
            else
            {
            	$demand = $employee->stopedemand();
            }
            
            
			$this->view_data['measure_dropdown'] = array();
			foreach($demand as $item)
			{
				if('' != $item->title)
				{
					$this->view_data['measure_dropdown'][$item->id] = $item->title;
				}
			}
            // Wenn bereits 3 Maßnahmen vorhanden sind...nicht die Möglichkeit eine weitere zu erstellen geben
            if(sizeof($this->view_data['measure_dropdown']) < 3 && $stope_id === FALSE)
            {
                $this->view_data['measure_dropdown']['0'] = '-- Nicht ersetzten sondern neu erstellen --';
            }
            
			$tags = Model\Tag::all();
			
			foreach($tags as $tag)
			{
				$this->view_data['tag_dropdown'][$tag->id] = $tag->name;
			}
			
			
            //$this->view_data['tag_dropdown'] = $this->teammember_model->get_tags_as_array();
            
            $this->view_data['teammember'] 				= $employee;
			
			$this->view_data['hidden']['teammember_id'] = $employee->id;
			$this->view_data['hidden']['course_id'] 	= $course_id;
			
        }
        
        $this->view_data['step'] = 2;
    }
    
    public function confirm()
    {
		$this->headline = 'Schritt 3: Daten überprüfen und bestätigen';
	
        $teammember_id = $this->input->post('teammember_id');
        $stope_id = $this->input->post('stope_id');
        $tag_id  = $this->input->post('tag_id');
        $this->view_data['replace'] = FALSE;
        $course_id = $this->input->post('course_id');
		
        if(isset($course_id) AND isset($teammember_id) AND isset($stope_id) AND ($this->_set_course_details($course_id)) AND isset($tag_id))
        {
            // Wenn neu erstellen ausgewählt wurde
            if($stope_id != 0)
            {
                $this->view_data['replace'] = TRUE;
				$stopedemand = Model\Stopedemand::find($stope_id);
				if($stopedemand->course_id !== NULL)
				{
				
					$course = $stopedemand->course();
					$this->view_data['m_name'] = $course->title;
				}
				else
				{
					$course = FALSE;
					$this->view_data['m_name'] = NULL;
					$this->view_data['demand_name'] = $stopedemand->title;
				}
				

            }
            
            $this->view_data['next'] = '/booking/do_it/';
            
            $this->view_data['btn_abort'] = array('title' => 'Abbrechen', 'target' => '/catalogue', 'options' => 'class="btn"');
            $this->view_data['btn_next'] = array('title' => 'Bestätigen', 'options' => 'class="btn btn-primary"');
            
			
			$employee = Model\Employee::find($teammember_id);
			
            $this->view_data['hidden']['course_id'] = $course_id;
            $this->view_data['hidden']['stope_id'] = $stope_id;
			$this->view_data['hidden']['tag_id'] = $tag_id;
			$this->view_data['hidden']['teammember_id'] = $employee->id;
			
			
            $this->view_data['teammember'] = $employee;
            $this->view_data['tag'] = Model\Tag::find($tag_id)->name;
            $this->view_data['step'] = 3;
        }
        else 
        {
            $this->view_data['error'] = 'Da ist irgendwas schief gelaufen. Massnahme wurde nicht gebucht!';
        }
    }
    
    public function do_it()
    {
        $this->headline = 'Buchung erfolgreich abgeschlossen';
        
        $course_id 		= $this->input->post('course_id');
        $stope_id 		= $this->input->post('stope_id');
        $teammember_id 	= $this->input->post('teammember_id');
        $tag_id  		= $this->input->post('tag_id');
        
        if($stope_id != 0)
        {
			$stopedemand = Model\Stopedemand::find($stope_id);
			$stopedemand->tag_id = $tag_id;
			$stopedemand->course_id = $course_id;
			if($stopedemand->save())
            {
                $this->view_data['success'] = 'Bedarf wurde erfolgreich getauscht!';
                $this->headline = 'Buchung erfolgreich abgeschlossen';
            }
            else $this->view_data['error'] = 'Bedarf konnte nicht ausgetauscht werden!';
        }
        else
        {
			
			$demand = Model\Stopedemand::where(array('employee_id' => $teammember_id,'course_id' => $course_id))->all();
			if($demand)
			{
				$this->view_data['error'] = 'Bedarf zu diesem Kurs existiert bereits, ändern Sie diesen!';
				echo $this->db->last_query();
				$this->headline = 'Buchung abgebrochen';
			}
			else
			{
				$data = array(
					'title' => 'Zusätzlicher Bedarf',
					'employee_id' => $teammember_id,
					'inactive' => 0,
					'tag_id' => $tag_id,
					'course_id' => $course_id
				);
				// Create Stope
				$stopedemand = Model\Stopedemand::make($data);
				if($stopedemand->save())
				{
					$this->view_data['success'] = 'Bedarf wurde erfolgreich erstellt!';
					$this->headline = 'Buchung erfolgreich abgeschlossen';
				}
				else $this->view_data['error'] = 'Bedarf konnte nicht erstellt werden!';
			}
        }
        
    }
    
    private function _set_course_details($course_id)
    {
        if ('' != $course_id) 
        {
            $this->view_data['course_details']['row'] = Model\Course::find($course_id);//$this->catalogue_model->get_specific($id);
            return TRUE;
        } 
        else
            return FALSE;
    }
    
}

